<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>盘点辅助</title>
</head>
<body>
<div id="stockTakingHelp_toolbar" style="text-align: center;width: 100%;height: auto;background: #fdfdfd">
    <p style="font-size: 30px;color: #0081c2">盘点辅助</p>
    <div style="width: 100%;height: auto;margin-bottom: 20px">
        <label>组 织</label>
        <input class="easyui-combobox"
               data-options="url:'/stockTaking/getPKOrgHelp',valueField:'pkOrg',textField:'name'"
               id="stockTakingView_PKOrgHelp"
               required="required">
        <label style="margin-left: 20px">日期</label>
        <input id="stockTakingView_Date" style="width: 150px">
        <a class="easyui-linkbutton" href="#" id="getBdStordocHelp" style="width: 90px;height: 30px;margin-left: 20px">仓库选择</a>
        <a class="easyui-linkbutton" data-options="iconCls:'icon-add'" href="#"
           id="stockTakingGenerate"
           style="width: 100px;height: 30px;margin-left: 20px">盘点票生成</a>
        <a class="easyui-linkbutton" data-options="iconCls:'icon-edit'" href="#"
           id="stockTakingViewEditor"
           style="width: 90px;height: 30px;margin-left: 20px">查看编辑</a>
        <a class="easyui-linkbutton" data-options="iconCls:'icon-search'" href="#"
           id="stockTakingResult"
           style="width: 110px;height: 30px;margin-left: 20px">盘盈亏表生成</a>
        <a class="easyui-linkbutton" style="width: 90px;height: 30px;margin-left: 20px"
           onclick="stockTakingExcel()" >导出excel</a>
    </div>
</div>
<table class="easyui-datagrid" data-options="singleSelect: true,toolbar: '#stockTakingHelp_toolbar'"
       id="stockTakignResult_table"
       style="width:100%;height:645px">
    <thead>
    <tr>
        <th data-options="field:'org',width:130,align:'center'">组织</th>
        <th data-options="field:'cmaterialcode',width:100,align:'center'">物料编号</th>
        <th data-options="field:'cmaterialname',width:200,align:'center'">物料名称</th>
        <th data-options="field:'cunitid',width:60,align:'center'">单位</th>
        <th data-options="field:'materialtype',width:80,align:'center'">型号</th>
        <th data-options="field:'materialclass',width:80,align:'center'">材料分类</th>
        <th data-options="field:'cwarehouseid',width:180,align:'center'">仓库</th>
        <th data-options="field:'clocationid',width:80,align:'center'">货位</th>
        <th data-options="field:'vbatchcode',width:100,align:'center'">批次号</th>
        <th data-options="field:'nnum',width:70,align:'center'">输入数量</th>
        <th data-options="field:'nonhandnum',width:70,align:'center'">现存量</th>
        <th data-options="field:'difference',width:70,align:'center'">差异数量</th>
        <th data-options="field:'vbillcode',width:90,align:'center'">盘点号</th>
    </tr>
</table>
<!--查看票号-->
<div class="easyui-dialog" closed="true" id="stockTakingViewCode" style="width: 1330px;height: 600px">
    <div id="stockTakingViewCode_toolbar">
        <label style="margin: 20px">日期</label>
        <input id="stockTakingViewCode_Date" style="width: 150px">
        <label style="margin: 20px">盘点票号</label>
        <input id="stockTakingViewCode_Code" style="width: 150px;height: 30px;margin: 20px" value="No.">
        <a class="easyui-linkbutton" data-options="iconCls:'icon-search'" href="#"
           onclick="stockTakingViewCode_Search()"
           style="margin-left: 30px;width: 100px">搜索</a>
    </div>
    <table class="easyui-datagrid" data-options="singleSelect: true,toolbar: '#stockTakingViewCode_toolbar'"
           id="stockTakingViewCode_table" style="width:100%;height:100%">
    </table>
</div>
<!--盘点票号生成-->
<div class="easyui-dialog" closed="true" id="stockTakingGenerate_dlg"
     style="width: 620px;height: auto;padding:10px 20px">
    <div style="margin-bottom: 10px">
        <label>日期</label>
        <input id="stockTakingHelp_Date" style="width: 100px">
        <label style="margin-left: 10px">起始票号:No.</label>
        <input id="stockTakingCode" style="width: 80px;height: 30px" type="text" value="10000000">
        <label style="margin-left: 10px">生成数量</label>
        <input id="stockTakingCount" style="width: 60px;height: 30px" type="text" value="100">
        <a class="easyui-linkbutton" href="#" iconCls="icon-ok" id="stockTakingGenerate_method"
           style="margin-left: 20px">生产票号</a>
    </div>
</div>
<!--仓库选择-->
<div buttons="#getBdStordocHelp_dlgButtons" class="easyui-dialog" closed="true" id="getBdStordoc_dlg"
     style="width:450px;height:300px">
    <table class="easyui-datagrid" id="StordocHelp_table" idField="pkStordoc" rownumbers="true"
           style="width:100%;height:200px" url="/stockTaking/getBdStordoc">
        <thead>
        <tr>
            <th data-options="field:'pkStordoc',width:50,checkbox:'true'">主键</th>
            <th data-options="field:'code',width:100,align:'center'">编号</th>
            <th data-options="field:'name',width:250,align:'center'">仓库名称</th>
        </tr>
        </thead>
    </table>
</div>
<div id="getBdStordocHelp_dlgButtons">
    <a class="easyui-linkbutton" href="#" iconCls="icon-ok" onclick="save_BdStordocHelp_dlg()">保存</a>
    <a class="easyui-linkbutton" href="#" iconCls="icon-cancel"
       onclick="javascript:$('#getBdStordoc_dlg').dialog('close')">取消</a>
</div>
<script type="text/javascript" src="/js/datagrid-export.js"></script>
<script th:inline="none" type="text/javascript">

    //仓库编号
    var BdStordoc_pkStordoc = []
    //票号
    var Code_Generate = []

    //生成票号窗口
    $("#stockTakingGenerate").click(function () {
        $("#stockTakingGenerate_dlg").dialog('open').dialog('setTitle', '生成票号')
    })

    //生产票号
    $("#stockTakingGenerate_method").click(function () {
        var date = $("#stockTakingHelp_Date").val();
        var startCode = $("#stockTakingCode").val();
        var count = $("#stockTakingCount").val();
        if (date != '' && startCode != '' && count != '') {
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "/stockTaking/stockTakingGenerate",
                data: {
                    date: date,
                    startCode: startCode,
                    count: count
                },
                success: function (data) {
                    if (data.statu == 404) {
                        warnSessionConfirm()
                    } else {
                        topCommonMessage(data.message);
                        $('#stockTakingGenerate_dlg').dialog('close');
                    }
                },
            })
        } else {
            topCommonMessage("请输入(日期/起始票号/生成数量)!");
        }
    })

    //时间格式化
    $(function () {
        showDate("stockTakingHelp_Date");
        showDate("stockTakingView_Date");
        showDate("stockTakingViewCode_Date");
    });

    //选择仓库窗口
    $("#getBdStordocHelp").click(function () {
        $('#getBdStordoc_dlg').dialog('open').dialog('setTitle', '选择仓库');
    })

    //选择的仓库编号
    function save_BdStordocHelp_dlg() {
        BdStordoc_pkStordoc = [];
        var rows = $("#StordocHelp_table").datagrid('getSelections');
        for (var i = 0; i < rows.length; i++) {
            BdStordoc_pkStordoc.push(rows[i].pkStordoc)
        }
        $("#getBdStordoc_dlg").dialog('close');
    }

    //查看编辑票号
    $("#stockTakingViewEditor").click(function () {
        $('#stockTakingViewCode').dialog('open').dialog('setTitle', '查看编辑盘点票号');
        $("#stockTakingViewCode_table").datagrid({
            url: '/stockTaking/stockTakingViewCode',
            rownumbers: true,
            pagination: true,
            idField: 'vbillcode',
            columns: [[
                {field: 'vbilldate', title: '日期', width: 100},
                {field: 'vbillcode', title: '盘点票号', width: 150},
                {
                    field: 'cmaterialcode', title: '物料编号', width: 150,
                    editor: {
                        type: 'textbox',
                        options: {
                            onChange: function () {//输入物料编号，物料名称联动
                                var editor = $('#stockTakingViewCode_table').datagrid('getEditors', getRowIndex(this));
                                var materialCode = $(editor[0].target).textbox('getText');
                                if (materialCode != '') {
                                    $.post({
                                        url: '/stockTaking/getmaterialName?materialCode=' + materialCode,
                                        success: function (data) {
                                            if (data == null || data == '') {
                                                topCommonMessage("没有找到这个物料编号!请检查是否填写正确!")
                                            }
                                            $(editor[1].target).val(data);
                                        }
                                    })
                                }
                            },
                            required:true
                        }
                    }
                },
                {field: 'cmaterialname', title: '物料名称', width: 200, editor: {type: 'text'}},
                {field: 'vbatchcode', title: '批次', width: 150, editor: 'text'},
                {field: 'cwarehouseid', title: '仓库', width: 200,
                    editor: {
                        type:'combobox',
                        options: {
                            valueField: 'name',
                            textField: 'name',
                            url: '/stockTaking/getBdStordoc',
                            required:true
                        }
                    }},
                {field: 'clocationid', title: '货位', width: 130, editor: 'text'},
                {field: 'nnum', width: 100, title: '数量', width: 100, editor: 'numberbox'},
                {
                    field: 'action', title: '操作', width: 80, align: 'center',
                    formatter: function (value, row, index) {
                        if (row.editing) {
                            var s = '<a href="#" onclick="stockTakingEditorSaverow(this, ' + index + ' )">保存</a> ';
                            var c = '<a href="#" onclick="stockTakingEditorCancelrow(this)">取消</a>';
                            return s + c;
                        } else {
                            var e = '<a href="#" onclick="stockTakingEditorEditrow(this)">编辑</a> ';
                            var d = '<a href="#" onclick="stockTakingEditorDeleterow(this)">删除</a>';
                            return e + d;
                        }
                    }
                }
            ]],
            onBeforeEdit: function (index, row) {
                row.editing = true;
                $(this).datagrid('refreshRow', index);
            },
            onAfterEdit: function (index, row) {
                row.editing = false;
                $(this).datagrid('refreshRow', index);
            },
            onCancelEdit: function (index, row) {
                row.editing = false;
                $(this).datagrid('refreshRow', index);
            }
        })
    });


    function stockTakingEditorEditrow(target) {
        $('#stockTakingViewCode_table').datagrid('beginEdit', getRowIndex(target));
    }

    function stockTakingEditorDeleterow(target) {
        $.messager.confirm('提示', '您确定要删除吗?', function (r) {
            if (r) {
                $('#stockTakingViewCode_table').datagrid('selectRow', getRowIndex(target));// 关键在这里 不要使用index会产生不同步
                var row = $('#stockTakingViewCode_table').datagrid('getSelected');
                $.post({
                    url: '/stockTaking/stockTakingDelete?vbillcode=' + row.vbillcode,
                    contentType: 'application/json'
                }, function (data) {
                    if (data.statu == 404) {
                        warnSessionConfirm();
                    } else {
                        $('#stockTakingViewCode_table').datagrid('deleteRow', getRowIndex(target));
                        topCommonMessage(data.message);
                    }
                })
            }
        });
    }

    function stockTakingEditorSaverow(target, index) {
        $('#stockTakingViewCode_table').datagrid('endEdit', getRowIndex(target));
        $('#stockTakingViewCode_table').datagrid('selectRow', index);// 关键在这里
        var row = $('#stockTakingViewCode_table').datagrid('getSelected');
        if (row) {
            $.post({
                url: '/stockTaking/stockTakingUpdate',
                contentType: 'application/json',
                data: JSON.stringify(row),
                success: function (data) {
                    if (data.statu == 404) {
                        warnSessionConfirm();
                    } else {
                        windowsOperation(null, null, data);
                    }
                }
            })
        }
    }

    function stockTakingEditorCancelrow(target) {
        $('#stockTakingViewCode_table').datagrid('cancelEdit', getRowIndex(target));
    }

    //搜索查看票号
    function stockTakingViewCode_Search() {
        $("#stockTakingViewCode_table").datagrid('load', {
            vbilldate: $("#stockTakingViewCode_Date").val(),
            vbillcode: $("#stockTakingViewCode_Code").val()
        })
    }


    //盘盈亏表生成
    $("#stockTakingResult").click(function () {

        var PKOrgHelp = $('#stockTakingView_PKOrgHelp').val();
        var stockTakingTime = $('#stockTakingView_Date').val();
        if (PKOrgHelp != '' && stockTakingTime != '' && BdStordoc_pkStordoc.length > 0) {
            $('#stockTakignResult_table').datagrid('load', {
                PKOrgHelp: PKOrgHelp,
                stockTakingTime: stockTakingTime,
                BdStordoc: BdStordoc_pkStordoc,
            });
            $('#stockTakignResult_table').datagrid({
                url: '/stockTaking/stockTakingResult',
                rownumbers: true,
                onLoadSuccess: function (data) {
                    if (data != null) {
                        $.messager.show({
                            title: '提示',
                            msg: data.msg,
                            timeout: 5000,
                            showType: 'slide'
                        });
                    }
                }
            })
        } else {
            topCommonMessage("请填写好必填信息(组织 / 日期 / 选择仓库)!")
        }
    })

    //导出excel
    function stockTakingExcel(){
        if ($("#stockTakignResult_table").datagrid("getData").total >0){
            $('#stockTakignResult_table').datagrid('toExcel','盘盈亏表.xls');
        }else {
            topCommonMessage("没有数据可导出!")
        }
    }
</script>
</body>
</html>